﻿Public Class frmNhanVien
    Shared nhanVien As New CNhanVien
    Shared da As New DataProvider
    Private Sub frmNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgvDSNV.DataSource = nhanVien.LoadDSNV()
        Dim Sql = "select distinct(GioiTinh) from NhanVien"
        cbxPhai.DataSource = CKiemTra.LoadDS(Sql)
        txtTimKiem.Focus()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click

        nhanVien.TenNV = txtHoTen.Text
        nhanVien.Phai = cbxPhai.Text
        nhanVien.NgaySinh = txtNgaySinh.Text
        nhanVien.DiaChi = txtDiaChi.Text
        nhanVien.DienThoai = txtDienThoai.Text
        nhanVien.InsertNhanVien(nhanVien)
        dgvDSNV.DataSource = nhanVien.LoadDSNV()
    End Sub

    Private Sub dgvDSNV_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSNV.SelectionChanged
        Dim index = dgvDSNV.CurrentRow.Index
        Dim row = dgvDSNV.Rows.Item(index)
        txtMaNV.Text = row.Cells(0).Value.ToString()
        txtHoTen.Text = row.Cells(1).Value.ToString()
        txtNgaySinh.Text = row.Cells(2).Value.ToString()
        cbxPhai.Text = row.Cells(3).Value.ToString()
        txtDiaChi.Text = row.Cells(4).Value.ToString()
        txtDienThoai.Text = row.Cells(5).Value.ToString()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim index = dgvDSNV.CurrentRow.Index
        nhanVien.DeleteNhanVien(dgvDSNV.CurrentRow.Cells(0).Value)
        dgvDSNV.Rows.RemoveAt(index)

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        nhanVien.MaNV = txtMaNV.Text
        nhanVien.TenNV = txtHoTen.Text
        nhanVien.Phai = cbxPhai.Text
        nhanVien.NgaySinh = txtNgaySinh.Text
        nhanVien.DiaChi = txtDiaChi.Text
        nhanVien.DienThoai = txtDienThoai.Text
        nhanVien.UpdateNhanVien(nhanVien)
        dgvDSNV.DataSource = nhanVien.LoadDSNV()
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        dgvDSNV.DataSource = CKiemTra.LoadDS("select * from NHANVIEN where TenNhanVien like N'%" + txtTimKiem.Text + "%'")
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        dgvDSNV.DataSource = nhanVien.LoadDSNV()
    End Sub

    Private Sub txtTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTimKiem.Click
        txtTimKiem.Text = ""
    End Sub

    Private Sub txtTimKiem_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTimKiem.KeyDown
        If e.KeyData = Keys.Enter Then
            btnTimKiem_Click(sender, e)
        End If
    End Sub
End Class